﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.ComponentModel;

namespace Chromatics.Pages.Tiles
{
    public partial class Tile : UserControl, INotifyPropertyChanged
    {
        private string iconPath;
        private LevelStatus levelStatus;
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public Tile()
        {
            InitializeComponent();
            this.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            this.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        }

        public void Run()
        {
            this.TapAnimation.Begin();
        }

        public void SetDisabled()
        {
            this.tile.IsEnabled = false;
        }

        public void SetEnabled()
        {
            this.tile.IsEnabled = true;
        }

        public LevelStatus GetStatus()
        {
            return this.levelStatus;
        }

        public void SetStatus(LevelStatus status)
        {
            this.levelStatus = status;

            if (status == LevelStatus.Closed)
            {
                this.IconPath = TileStatus.Locked;
                this.SetDisabled();
            }
            else if (status == LevelStatus.Open)
            {
                this.IconPath = TileStatus.Open;
                this.SetEnabled();
            }
            else if (status == LevelStatus.Star0)
            {
                this.IconPath = TileStatus.Passed;
            }
            else if (status == LevelStatus.Star1)
            {
                this.IconPath = TileStatus.OneStar;
            }
            else if (status == LevelStatus.Star2)
            {
                this.IconPath = TileStatus.TwoStars;
            }
            else if (status == LevelStatus.Star3)
            {
                this.IconPath = TileStatus.ThreeStars;
            }
            else
            {
                throw new Exception("Level status error");
            }
            
        }

        public string IconPath
        {
            get
            {
                return this.iconPath;
            }
            set
            {
                this.iconPath = value;
                NotifyPropertyChanged();
            }
        }

        public Thickness TileMargin
        {
            get
            {
                return this.Margin;
            }

            set
            {
                this.Margin = value;
            }
        }

        public string TileContent
        {
            get
            {
                return this.tile.Content.ToString();
            }

            set
            {
                this.tile.Content = value;
            }
        }

        public Brush TileBackground
        {
            get
            {
                return this.tile.Background;
            }
            set
            {
                this.tile.Background = value;
            }
        }

        public Brush TileBorderBrush
        {
            get
            {
                return this.tile.BorderBrush;
            }
            set
            {
                this.tile.BorderBrush = value;
            }
        }
    }
}
